package problems.contest;

/**
 * 第 316 场周赛
 * <p>https://leetcode.cn/contest/weekly-contest-316</p>
 *
 * @author habitplus
 * @since 10:29, 2022/10/23
 */
public class WCT316 {

    /**
     * T1|2446. 判断两个事件是否存在冲突
     * <p>https://leetcode.cn/problems/determine-if-two-events-have-conflict/</p>
     */
    public boolean haveConflict(String[] event1, String[] event2) {
        int st1 = transMinute(event1[0]);
        int ed1 = transMinute(event1[1]);
        int st2 = transMinute(event2[1]);
        int ed2 = transMinute(event2[1]);

        if (ed1 < st2 || ed2 < st1) return false;
        return true;
    }

    private int transMinute(String s) {
        int h = Integer.parseInt(s.substring(0, 2));
        int m = Integer.parseInt(s.substring(3));
        return h * 60 + m;
    }


    /**
     * T2|2447. 最大公因数等于 K 的子数组数目
     * <p>https://leetcode.cn/problems/number-of-subarrays-with-gcd-equal-to-k/</p>
     * <li style="color: red">WA (Wrong Answer)</li>
     */
    public int subarrayGCD(int[] nums, int k) {
        int n = nums.length;
        int ret = 0;
        int left, right;
        int i = 0;
        int cnt = 0;

        while (i < n) {
            if (nums[i] == k) {
                cnt = 0;
                left = i - 1;
                while (left >= 0 && nums[left] % k == 0) {
                    cnt += nums[left] == k ? 1: 0;
                    --left;
                }

                right = i+1;
                while (right < n && nums[right] % k == 0) {
                    cnt += nums[right] == k ? 1 : 0;
                    ++right;
                }

                ret += (right - left - cnt - 1);

                i = right;
            }
        }

        return ret;
    }
}
